﻿using GameBoard;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
using System.Windows.Media.Media3D;

namespace CombatObjectWrapper
{
    class CombatObjectViewModel : INotifyPropertyChanged
    {

        private GameBoard.GameBoardVisual _viewport;
        public CombatObjectViewModel(GameBoard.GameBoardVisual _viewport)
        {
            this._viewport = _viewport;
        }
        private double _scaleValue = 1;

        public double ScaleValue
        {
            get { return _scaleValue; }
            set 
            {
                _scaleValue = value;
                OnPropertyChanged("ScaleValue");
                RedoVisual();
            }
        }

        private double _rotationValue = 90;

        public double RotationValue
        {
            get { return _rotationValue; }
            set 
            { 
                _rotationValue = value;
                OnPropertyChanged("RotationValue");
                RedoVisual();
            }
        }

        private String _modelObjectFile;

        public String ModelObjectFile
        {
            get { return _modelObjectFile; }
            set 
            {
                _modelObjectFile = value;
                OnPropertyChanged("ModelObjectFile");
                RedoVisual();
            }
        }

        public ObjectVisualData CurrentData { get; set; }
        private void RedoVisual()
        {
            if (CurrentModelID != null)
            {
                _viewport.RemoveVisual(CurrentModelID);
            }
            CurrentModelID = Guid.NewGuid();
            if(
                String.IsNullOrWhiteSpace(ModelObjectFile))
            {
                return;
            }
            CurrentData = new ObjectVisualData()
            {
                Angle = RotationValue,
                Scale = ScaleValue,
                MaterialFile = MaterialFile,
                MaterialColor = ColorChosen,
                ObjectFile = ModelObjectFile,
            };
            ObjectVisualFactory objfact = new ObjectVisualFactory(CurrentData);
            _viewport.AddVisual(CurrentModelID, objfact.MakeVisual());
        }
        
        

        #region INotifyPropertyChanged Members

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string name)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(name));
            }
        }

        #endregion


        private Guid CurrentModelID { get; set; }
        private string _materialFile = "3DObjects\\ObjectVisuals\\Stone.jpg";

        public string MaterialFile
        {
            get
            {
                return _materialFile;
            }
            set
            {
                _materialFile = value;
                OnPropertyChanged("MaterialFile");
                RedoVisual();
            }
        }

        private Color _colorChosen;
        public Color ColorChosen 
        {
            get
            {
                return _colorChosen;
            }
            set
            {
                _colorChosen = value;
                _materialFile = "";
                OnPropertyChanged("ColorChosen");
                RedoVisual();
            }
        }
    }
}
